import spring.framework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fun.pengzh
 * @class PACKAGE_NAME.ProxyFactoryBean
 * @desc
 * @since 2021-10-23
 */
public class ProxyFactoryBean implements FactoryBean<IUserDAO> {


    @Override
    public IUserDAO getObject() throws Exception {
        InvocationHandler handler = (proxy, method, args) -> {
            if ("toString".equals(method.getName())) return this.toString();
            Map<String, String> hashMap = new HashMap<>();
            hashMap.put("10001", "空穴来风");
            hashMap.put("10002", "无法无天");
            hashMap.put("10003", "闻风丧胆");
            return "你被代理了 " + method.getName() + "：" + hashMap.get(args[0].toString());
        };

        return (IUserDAO) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IUserDAO.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDAO.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
